#!/bin/bash
# Author: fanyongfeng
# 安装nginx
# 操作系统：centos7/银河麒麟v10

set -e
# 使用root用户执行
[[ $(id -u) -ne 0 ]] && echo "请使用root用户执行此脚本" && exit 1

usage(){
    echo "Usage:"
    echo "sh $0
            --prefix=安装目录(默认/data/nginx)
            --version=安装版本(默认1.24.0)
        "
    echo "示例:"
    echo "sh $0 --prefix=/data/nginx --version=1.24.0"
    exit 1
}

#处理参数，规范化参数
ARGS=`getopt -o h --long prefix:,version:,help -n "$0" -- "$@"`
if [[ $? != 0 || $# -eq 0 ]]
then
        usage
        exit 1
fi

#重新排列参数顺序
eval set -- "${ARGS}"
#通过shift和while循环处理参数
while true
do
    case $1 in
        --prefix)
            PREFIX=$2
            shift 2
            ;;
        --version)
            VERSION=$2
            shift 2
            ;;
        -h|-help)
            usage
            ;;
         --)
            shift
            break
            ;;
         *)
            echo "Internal error!"
            exit 1
            ;;            
    esac
done



INSTALL_DIR=${PREFIX:-/data/nginx}
VERSION=${VERSION:-1.24.0}
RUN_USER="nginx"
HOST_IP=$(ip a | grep global | head -1 | awk -F'/' '{print $1}' | awk '{print $2}')

# 安装依赖
echo "正在安装依赖..."
yum install gcc libgcc make perl perl-devel zlib zlib-devel openssl openssl-devel -y
  
# 下载nginx
if [[ ! -f nginx-${VERSION}.tar.gz ]]
then
    echo "正在下载安装包..."
    wget -4 https://nginx.org/download/nginx-${VERSION}.tar.gz
    if [[ $? -ne 0 ]]
    then
        echo "nginx-${VERSION}.tar.gz下载失败，请重试或手动上传此安装包"
        exit 1
    fi
fi


# 安装nginx
echo "正在安装nginx..."
tar zxf nginx-${VERSION}.tar.gz && cd nginx-${VERSION}
# 配置vim支持nginx.conf语法高亮
\cp -r contrib/vim/* /usr/share/vim/vimfiles/

./configure --prefix=${INSTALL_DIR} --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-debug --with-stream
CPU_COUNTS=$(cat /proc/cpuinfo |grep processor | wc -l)
make -j${CPU_COUNTS} && make install
ln -sf ${INSTALL_DIR}/sbin/nginx /bin/nginx
 
id ${RUN_USER} || useradd ${RUN_USER} -s /sbin/nologin
chown -R ${RUN_USER}.${RUN_USER} ${INSTALL_DIR}


# 修改nginx配置文件
if [ "${CPU_COUNTS}" -le 8 ]
then
  worker_processes=auto
else
  worker_processes=8
fi
sed -i "s/#user  nobody;/user ${RUN_USER};/g" ${INSTALL_DIR}/conf/nginx.conf
sed -i "s/worker_processes  1;/worker_processes ${worker_processes};/g" ${INSTALL_DIR}/conf/nginx.conf
sed -i 's/worker_connections  1024;/worker_connections  65535;/g' ${INSTALL_DIR}/conf/nginx.conf
sed -i '/worker_connections/a\    use epoll;' ${INSTALL_DIR}/conf/nginx.conf

 
# 创建 service 文件，利用 systemd 控制 nginx
cat > /etc/systemd/system/nginx.service <<EOF
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
ExecStartPost=/bin/sleep 0.1
PIDFile=${INSTALL_DIR}/logs/nginx.pid
ExecStartPre=${INSTALL_DIR}/sbin/nginx -t
ExecStart=${INSTALL_DIR}/sbin/nginx
ExecReload=${INSTALL_DIR}/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT \$MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF

# 启动nginx
echo "安装完成，启动nginx..."
systemctl daemon-reload
systemctl enable nginx
systemctl start nginx
systemctl status nginx
